﻿using Dapper;
using ProTrack.Dto;
using ProTrack.Entity;
using ProTrack.IRepository;
using ProTrack.IRepository.WareHouse;
using ProTrack.IServices.Stock;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProTrack.Services.Stock
{
    public class OtherWareHouseServices : IOtherWareHouseServices
    {
        public OtherWareHouseServices(IBaseRepository db, IOtherWareHouseRepository ow)
        {
            Ow = ow;
            Db = db;
        }
        public IOtherWareHouseRepository Ow { get; }
        public IBaseRepository Db { get; }
        //新增
        public async Task<bool> AddAsync(OtherWareHouse model, List<OtherWareHouseItem> list)
        {
            var result = await Ow.AddOtherWareHouse(model, list);
            return result;
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>
        public async Task<bool> Delete(int[] Ids)
        {
            var result = await Ow.DeleteOtherWareHouse(Ids);
            return result;
        }

        /// <summary>
        /// 删除单条
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<bool> Delete(int Id)
        {
            var result = await Ow.DeleteOtherWareHouse(Id);
            return result;
        }

        /// <summary>
        /// 查询所有数据
        /// </summary>
        /// <returns></returns>
        public async Task<IEnumerable<OtherWareHouse>> GetAllAsync()
        {
            string sql = "select * from OtherWareHouse";
            var list = await Db.QueryAllAsync<OtherWareHouse>(sql);
            return list;
        }

        /// <summary>
        /// 分页获取 / 根据条件查询数据
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <param name="Number"></param>
        /// <param name="RealName"></param>
        /// <param name="WareHouseId">仓库Id</param>
        /// <param name="Status"></param>
        /// <returns></returns>
        public async Task<IEnumerable<OtherWareHouse>> GetAsync(int PageIndex, int PageSize, string Number, string RealName, string WareHouseId, string Status)
        {
            string sql = "select * from OtherWareHouse where 1=1 ";
            if (!string.IsNullOrEmpty(Number))
            {
                sql += " and Number like @Number ";
            }

            if (!string.IsNullOrEmpty(RealName))
            {
                sql += " and RealName like @RealName";
            }

            if (!string.IsNullOrEmpty(WareHouseId))
            {
                sql += " and WareHouseId like @WareHouseId";
            }


            if (!string.IsNullOrEmpty(Status) && !Status.Equals("全部"))
            {
                sql += " and Status = @Status";
            }

            sql += " order by CreateTime desc offset @PageSize*(@PageIndex-1) row fetch next @PageSize rows only";


            var list = await Db.QueryAsync<OtherWareHouse>(sql, new { PageIndex, PageSize, Number = $"%{Number}%", RealName = $"%{RealName}%", WareHouseId = $"%{WareHouseId}%", Status });
            return list;
        }

        /// <summary>
        /// 获取总条数
        /// </summary>
        /// <param name="Number"></param>
        /// <param name="RealName"></param>
        /// <param name="WareHouseId"></param>
        /// <param name="Status"></param>
        /// <returns></returns>
        public async Task<int> GetCountAsync(string Number, string RealName, string WareHouseId, string Status)
        {
            string sql = "select count(*) from OtherWareHouse where 1=1 ";
            if (!string.IsNullOrEmpty(Number))
            {
                sql += " and Number like @Number ";
            }

            if (!string.IsNullOrEmpty(RealName))
            {
                sql += " and RealName like @RealName";
            }

            if (!string.IsNullOrEmpty(WareHouseId))
            {
                sql += " and WareHouseId like @WareHouseId ";
            }
            if (!string.IsNullOrEmpty(Status) && !Status.Equals("全部"))
            {
                sql += " and Status = @Status";
            }
            var count = await Db.GetRecoreCountAsync(sql, new { Number = $"%{Number}%",RealName = $"%{RealName}%", WareHouseId = $"%{WareHouseId}%", Status });
            return count;
        }

        public async Task<IEnumerable<OtherWareHouseDto>> GetOtherWareHouseItemById(string OtherWareHouseId, string WareHouseId)
        {
            var list = await Db.QueryAsync<OtherWareHouseDto>(@"select ot.*,it.Spec,it.UnitOfMeasure,it.ItemCode,it.Id as ItemId,it.ItemName,it.Remark,ow.WareHouseId,
                    (isk.StockCount-isk.SalesCount) as CurrCount
                    from OtherWareHouseItem ot
                    inner join OtherWareHouse ow on ow.Id=ot.OtherWareHouseId
                    inner join Item it on it.Id = ot.ItemId
                    inner join ItemStock isk on isk.ItemId = it.Id and isk.WareHouseId = @WareHouseId
                    where ot.OtherWareHouseId =@OtherWareHouseId", new { OtherWareHouseId, WareHouseId });
            return list;
        }

        public async Task<bool> UpdateAsync(OtherWareHouse model, List<OtherWareHouseItem> list)
        {
            var result = await Ow.UpdateOtherWareHouse(model, list);
            return result;
        }

        public async Task<bool> UpdateStatus(long Id, int Status)
        {
            string sql = $"update OtherWareHouse set Status=@Status where Id =@Id";
            var result = await Db.UpdateAsync(sql, new { Id, Status });
            return result;
        }
    }
}
